﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETADTools.Lib
{
  public partial class Field
  {
    public string FieldName { get; set; }
    public IFieldType FieldType { get; set; }
    public bool IsNullable { get; set; }
    public string ResourceText { get; set; }

    /// <summary>
    /// Create a new Field
    /// </summary>
    /// <param name="fieldName"></param>
    /// <param name="fieldType"></param>
    public Field(string fieldName, IFieldType fieldType, string resourceText = "", bool isNullable = true)
    {
      FieldName = fieldName;
      FieldType = fieldType;      
      ResourceText = resourceText;

      IsNullable = isNullable;
    }

    /// <summary>
    /// Convert the field to sql script based on styles. 
    /// This one is suitable for most of common purposes.
    /// </summary>
    /// <param name="styles"></param>
    /// <returns></returns>
    public string ToSqlScript(params Style[] styles)
    {
      StringBuilder sql = new StringBuilder();

      // We only display field name by default
      sql.AppendFormat(FieldName);

      // Apply styles on field if any
      foreach (Style style in styles)
      {
        switch (style)
        {
          case Style.TypeAndSize:
            // Display field type
            sql.AppendFormat(" {0}", FieldType);

            // Also display size if available
            if (FieldType.GetSize() != string.Empty)
            {
              sql.AppendFormat(" {0}", FieldType.GetSize());
            }
            break;

          case Style.Parameter:
            // Display as parameter style
            sql.Insert(0, FieldType.ToParameterStyle());
            break;

          case Style.Declaration:
            // Display as delclaration style
            sql.Insert(0, FieldType.ToDeclareStyle());
            break;

          case Style.Nullable:
            // Display as nullable
            sql.AppendFormat(" {0}", GetNullable());
            break;

          case Style.Update:
            // Display as update style ex. Salary = @vdblSalary
            sql.AppendFormat(" = {0}{1}", FieldType.ToParameterStyle(), FieldName);
            break;

          default:
            break;
        }
      }

      return sql.ToString();
    }

    /// <summary>
    /// To use in SELECT query (i.e Emp.PID). 
    /// This is special so should not be deleted.
    /// </summary>
    /// <param name="tableAlias"></param>
    /// <returns></returns>
    public string ToSqlGetQuery(string tableAlias)
    {
      return string.Format("{0}.{1}", tableAlias, FieldName);
    }

    /// <summary>
    /// Get text for NULL or NOT NULL field
    /// </summary>
    /// <returns></returns>
    private string GetNullable()
    {
      string nullableCondition = string.Empty;

      // build NULL or NOT NULL field
      if (IsNullable == true)
      {
        nullableCondition = "NULL";
      }
      else
      {
        nullableCondition = "NOT NULL";
      }

      return nullableCondition;
    }
  }
}
